package sharewithus.common;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.springframework.stereotype.Controller;

import sharewithus.business.service.UserService;
import framelib.common.FrameCommon;
import framelib.common.SessionData;
import framelib.utils.ClientXmemcached;

/**
 * Title:SessionListener
 * Description:session监听类
 * @Create_by:Vince Yu
 * @Create_date:2012-5-11
 * @Last_Edit_By:
 * @Edit_Description
 * @version:ShareWithUs 1.0
 * 
 */
@Controller
public class SessionListener implements HttpSessionListener{
	
	public void sessionCreated(HttpSessionEvent event) {
		
	}

	/**
	 * 2012-5-11, 上午10:17:43
	 * 方法描述：监听session销毁事件
	 * @param event
	 */
	public void sessionDestroyed(HttpSessionEvent event) {
        HttpSession ses = event.getSession();
        if(ses.getAttribute(FrameCommon.PNN_SESSION) != null){
        	SessionData session = ((SessionData)ses.getAttribute(FrameCommon.PNN_SESSION));
            synchronized (this) {
            	UserService userService = (UserService)Common.factory.getBean("userService");
            	try {
	                userService.updateUserLastLogin(session.getId(), session.getCurrentLoginIp(), session.getCurrentLoginTime());
                } catch (Exception e) {
	                // TODO Auto-generated catch block
	                e.printStackTrace();
                }
            	
            	ClientXmemcached mc = (ClientXmemcached)Common.factory.getBean("clientMemcached");
            	mc.delete(session.getId() + FrameCommon.PNN_SESSION);
            }
        }
        
	}
}
